﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using ScannerDemo.common;
using System.Windows.Media;
using Windows.Storage;
using System.IO.IsolatedStorage;
using System.IO;

namespace ScannerDemo
{
    public partial class ScanResult : PhoneApplicationPage
    {
        string tempString = App.PublicText;
            
        public ScanResult()
        {
            InitializeComponent();

        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            //string tempString = App.PublicText;

                //NavigationContext.QueryString.TryGetValue("text", out tempString);
            //}
            if ( App.PublicText!=null)
                tempString = App.PublicText;

            if ((tempString == null) || (tempString == ""))
            {
                if (ConfigFileIsExists())
                {

                    tempString = GetAppConfigFile();

                }
                else
                {

                    return;
                }
            }

            ResultText.Text = tempString;
                  
                 //ResultText.Foreground = new SolidColorBrush(Colors.Yellow);
              //  ResultText.Background = new SolidColorBrush(Colors.Blue);
          
           // if (tempString.Contains("http://") || tempString.Contains(".com") || tempString.Contains(".cn"))
            ResultType.Text = CommonLib.get_type(tempString);
            if (ResultType.Text != "名片")
            {
                //HyperlinkButton1.NavigateUri = new Uri(tempString);
                //HyperlinkButton1.Content = tempString;
                //ResultText.IsReadOnly = true;
                OpenWithNB.Visibility = Visibility.Visible;
                //HyperlinkButton1.Visibility = Visibility.Visible;
                TextBoxBrowser.Visibility = Visibility.Visible;

                System.Windows.Media.Imaging.BitmapImage imageSource = new System.Windows.Media.Imaging.BitmapImage();
                imageSource.UriSource = new Uri("/icons/website.png", UriKind.Relative); ;
                //imageSource.SetSource("/icons/website.png");

                
               
            }
            else
            {
                OpenWithNB.Visibility = Visibility.Collapsed;
                //HyperlinkButton1.Visibility = Visibility.Collapsed; 
               
                TextBoxBrowser.Visibility = Visibility.Collapsed; 
                //ResultText.IsReadOnly = false;
                if (ResultType.Text == "名片")
                {
                    if (e.NavigationMode == NavigationMode.Back)
                    {
                        NavigationService.Navigate(new Uri("/HomePagePivot.xaml?pivotIndex=1", UriKind.Relative));
                    }

                }
            }

        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream = file.OpenFile(@"TempResult.dat", FileMode.OpenOrCreate, FileAccess.Write))
                {
                    StreamWriter streamWriter = new StreamWriter(fileStream);
                    streamWriter.WriteLine(tempString);
                    streamWriter.Flush();
                    streamWriter.Dispose();
                    streamWriter.Close();
                }
            }
            base.OnNavigatedFrom(e);

        }
        /// <summary>
        /// 判断软件的配置文件是否存在
        /// </summary>
        /// <returns>返回是否存在</returns>
        public bool ConfigFileIsExists()
        {
            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                return file.FileExists(@"TempResult.dat");
            }
        }
        /// <summary>
        /// 读取软件记录的客户端程序
        /// </summary>
        /// <returns>返回程序配置信息.如果不存在配置文件.则返回的是默认的配置信息</returns>
        public string GetAppConfigFile()
        {
            string strMapAppConfig = "";
            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream = file.OpenFile(@"TempResult.dat", FileMode.Open, FileAccess.Read))
                {
                    StreamReader streamReader = new StreamReader(fileStream);
                    strMapAppConfig = streamReader.ReadLine();
                    streamReader.Close();
                }
            }
            return strMapAppConfig;
        }
        private void OpenWithNB_Click_1(object sender, RoutedEventArgs e)
        {
            NavigateUriByLaunchNokiaBrowser();
            
        }
        async void NavigateUriByLaunchNokiaBrowser()
        {
            string uriString = "nokiabrowser2:";
            string baiduSearch = "http://m.baidu.com/s?from=1086k&word=";//http://m.baidu.com/s?word=
            if (ResultType.Text=="文本")
                uriString = uriString + baiduSearch + ResultText.Text;
            else
                uriString = uriString+ ResultText.Text;

            Uri uri = new Uri(uriString);

            try
            {
                bool success = await Windows.System.Launcher.LaunchUriAsync(uri);
                if (success)
                {
                    // URI launched
                }
                else
                {
                    // URI launch failed
                }
            }
            catch
            { 
            }
        }

        private void ResultText_MouseLeftButtonDown_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var tb = sender as TextBox;

            if (tb != null)
            {
                tb.SelectAll();
            }

        }

        private void ResultText_MouseLeftButtonUp_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var tb = sender as TextBox;

            if (tb != null)
            {
                tb.SelectAll();
            }
            
        }

        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/share_info.xaml?text="+ResultText.Text, UriKind.Relative));
        }

        private void ApplicationBarIconButton_Click_2(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void ApplicationBarIconButton_Click_HomePage(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/HomePagePivot.xaml", UriKind.Relative));
        }

        private void ResultText_GotFocus_1(object sender, RoutedEventArgs e)
        {
            var tb = sender as TextBox;
            if (tb != null)
            {
               tb.SelectAll();
                
            }
        }
        /*
        private void ResultImage_MouseLeftButtonDown_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            string uriToLaunch = ResultText.Text;
            var uri = new Uri(uriToLaunch);
            DefaultLaunch(uri);
        }
         */
        async void DefaultLaunch(Uri uri)
        {
            // Launch the URI
            var success = await Windows.System.Launcher.LaunchUriAsync(uri);

            if (success)
            {
                // URI launched
            }
            else
            {
                // URI launch failed
            }
        }



        private void ResultText_MouseEnter_1(object sender, System.Windows.Input.MouseEventArgs e)
        {
            var tb = sender as TextBox;

            if (tb != null)
            {
                tb.SelectAll();
            }
        }

        private void ResultText_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
        {
            var tb = sender as TextBox;

            if (tb != null)
            {
                tb.SelectAll();
            }
        }

        private void ResultText_TextChanged(object sender, TextChangedEventArgs e)
        {
            
        }

       
    }
}